package com.gxg.dockerapi;

import java.io.IOException;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class DockerContainerAPI
{
  static OkHttpClient client = new OkHttpClient();
  
  public static String queryContainersInfo(String dockerServerIP)
    throws IOException
  {
    Request request = new Request.Builder().get().url(dockerServerIP + "/containers/json?all=true").build();
    Response response = null;
    try
    {
      response = client.newCall(request).execute();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return response.body().string();
  }
  
  public static int createContainer(String dockerServerIP, String containerName, String imageID, String hostPort)
  {
    String jsonString = "{\"image\":\"" + imageID + "\", \"HostConfig\":{\"PortBindings\": { \"22/tcp\": [{ \"HostPort\": \"" + hostPort + "\" }] }} }";
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonString);
    
    Request request = new Request.Builder().post(requestBody).url(dockerServerIP + "/containers/create?name=" + containerName).build();
    Response response = null;
    try
    {
      response = client.newCall(request).execute();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return response.code();
  }
  
  public static int startContainer(String dockerServerIP, String containerID)
  {
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "temp");
    
    Request request = new Request.Builder().post(requestBody).url(dockerServerIP + "/containers/" + containerID + "/start").build();
    Response response = null;
    try
    {
      response = client.newCall(request).execute();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return response.code();
  }
  
  public static int restartContainer(String dockerServerIP, String containerID)
  {
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "temp");
    
    Request request = new Request.Builder().post(requestBody).url(dockerServerIP + "/containers/" + containerID + "/restart").build();
    Response response = null;
    try
    {
      response = client.newCall(request).execute();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return response.code();
  }
  
  public static int killContainer(String dockerServerIP, String containerID)
  {
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "temp");
    
    Request request = new Request.Builder().post(requestBody).url(dockerServerIP + "/containers/" + containerID + "/kill").build();
    Response response = null;
    try
    {
      response = client.newCall(request).execute();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return response.code();
  }
  
  public static int removeContainer(String dockerServerIP, String containerID)
  {
    Request request = new Request.Builder().delete().url(dockerServerIP + "/containers/" + containerID).build();
    Response response = null;
    try
    {
      response = client.newCall(request).execute();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return response.code();
  }
}
